Skip to content

perf(database): index BatchTaskRun on (runtimeEnvironmentId, createdAt, id) for the batches list#4361

Merged
ericallam merged 1 commit into
mainfrom
perf/batch-list-created-at-index
Jul 24, 2026
Merged

perf(database): index BatchTaskRun on (runtimeEnvironmentId, createdAt, id) for the batches list#4361
ericallam merged 1 commit into
mainfrom
perf/batch-list-created-at-index

Conversation

@ericallam

Copy link
Copy Markdown
Member

Summary

The batches list page orders by createdAt DESC, id DESC filtered by environment and a created-at window, but the only supporting index on BatchTaskRun was (runtimeEnvironmentId, id). That index can't satisfy the createdAt ordering, so on environments with a large number of batches the query fell back to a full table scan and in-memory sort, which could run long enough to hit the statement timeout.

Fix

Adds (runtimeEnvironmentId, createdAt DESC, id DESC) on BatchTaskRun. The query now reads straight from the index in order with no sort step, returning a page with only a handful of heap fetches instead of scanning the whole environment slice.

The migration uses CREATE INDEX CONCURRENTLY IF NOT EXISTS, so it takes no table lock and is a no-op if the index already exists.

…t, id) for the batches list

The batches list page orders by createdAt DESC, id DESC filtered by
environment and a created-at window, but the only matching index was
(runtimeEnvironmentId, id), which cannot satisfy the createdAt ordering.
On environments with many batches the query fell back to a full table
scan and in-memory sort, which could run long enough to hit the
statement timeout.

Adds (runtimeEnvironmentId, createdAt DESC, id DESC) so the query reads
straight from the index in order. Created with CONCURRENTLY IF NOT
EXISTS, so it takes no table lock and is a no-op if already present.
@changeset-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: afe280c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ericallam
ericallam marked this pull request as ready for review July 24, 2026 10:29

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review

Comment thread internal-packages/database/prisma/schema.prisma
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds a composite descending index on BatchTaskRun covering runtimeEnvironmentId, createdAt, and id. The Prisma schema and migration define the same index, with the migration creating it concurrently and idempotently. A changelog entry records the Batches list performance fix.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers summary and fix, but it omits required template sections like Closes #, checklist, testing, changelog, and screenshots. Add the missing template sections: Closes #, checklist items, Testing steps, Changelog, and Screenshots or an explicit note if unavailable.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly describes the main change: adding a BatchTaskRun index for the batches list performance fix.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/batch-list-created-at-index

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ericallam
ericallam merged commit 9c85e0e into main Jul 24, 2026
38 checks passed
@ericallam
ericallam deleted the perf/batch-list-created-at-index branch July 24, 2026 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants